import java.util.PriorityQueue;

/*
 * @lc app=leetcode.cn id=703 lang=java
 *
 * [703] 数据流中的第 K 大元素
 */

// @lc code=start
class KthLargest {

  int k;
  PriorityQueue<Integer> queue;

  public KthLargest(int k, int[] nums) {
    this.k = k;
    queue = new PriorityQueue<>(k, (a, b) -> a - b);
    for (int num : nums) {
      add(num);
    }
  }

  public int add(int val) {
    if (queue.size() < k) {
      queue.add(val);
    } else {
      if (val > queue.peek()) {
        queue.poll();
        queue.add(val);
      }
    }
    return queue.peek();
  }
}

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest obj = new KthLargest(k, nums);
 * int param_1 = obj.add(val);
 */
// @lc code=end
